Imports Microsoft.VisualBasic
Imports System.Text
Imports SQLtoASPX.Support

Public Class DetailASPXVB
    Private TableGen As RawCreateScript
    'Private mDatabaseName As String

    Public Sub New(ByVal TableGen As RawCreateScript)
        Me.TableGen = TableGen
    End Sub

    Public Overrides Function ToString() As String
        Dim sb As New StringBuilder
        Dim SQL As String = ""


        Dim Spaces As New Space
        Select Case TableGen.CodeType
            Case CodeTypeEnum.VB2005
                If TableGen.IsAddImports Then
                    sb.AppendLine("Imports " & GetDatabaseImportLine(TableGen))
                    sb.AppendLine()
                End If
        End Select

        Select Case TableGen.CodeType
            Case CodeTypeEnum.CS2005
                sb.AppendLine("using System;")
                sb.AppendLine("using System.Data;")
                sb.AppendLine("using System.Configuration;")
                sb.AppendLine("using System.Collections;")
                sb.AppendLine("using System.Web;")
                sb.AppendLine("using System.Web.Security;")
                sb.AppendLine("using System.Web.UI;")
                sb.AppendLine("using System.Web.UI.WebControls;")
                sb.AppendLine("using System.Web.UI.WebControls.WebParts;")
                sb.AppendLine("using System.Web.UI.HtmlControls;")
                sb.AppendLine("using " & GetDatabaseImportLine(TableGen) & ";")
                sb.AppendLine("using System.Text.RegularExpressions;")
                sb.AppendLine("")
        End Select


        Select Case TableGen.CommentLevel
            Case CommentLevelEnum.Basic

            Case CommentLevelEnum.Verbose
                AddComment(sb, TableGen, Spaces, "Class declaration for Edit" & TableGen.TableName)

        End Select
      
        Select Case TableGen.CodeType
            Case CodeTypeEnum.VB2005
                sb.AppendLine("Partial Class Edit" & TableGen.ClassName)
                sb.AppendLine("    Inherits System.Web.UI.Page")
                sb.AppendLine()
            Case CodeTypeEnum.CS2005
                sb.AppendLine("public partial class Edit" & TableGen.ClassName & " : System.Web.UI.Page")
                sb.AppendLine("{")
        End Select

        sb.Append(GetDetailASPXPageLoad(TableGen))

        GetDetailASPXDeleteClick(Spaces, sb, TableGen)

        Spaces.Inc()
        GetDetailASPXSaveClick(Spaces, sb, TableGen)
        Spaces.Dec()

        sb.AppendLine()

        Select Case TableGen.CodeType
            Case CodeTypeEnum.CS2005
                sb.Append(GetSessionValueFunction(TableGen))
                sb.Append(GetRequestValueFunction(TableGen))
        End Select

        sb.Append(GetDetailASPXCancelClick(TableGen))

        Select Case TableGen.CodeType
            Case CodeTypeEnum.CS2005
                sb.AppendLine()
                GetIsNumericFunction(Spaces, sb, TableGen)
                GetIsDateFunction(Spaces, sb, TableGen)
        End Select


        Select Case TableGen.CommentLevel
            Case CommentLevelEnum.Basic

            Case CommentLevelEnum.Verbose
                AddComment(sb, TableGen, Spaces, " ")
                AddComment(sb, TableGen, Spaces, "End Class Edit" & TableGen.TableName)

        End Select
     
        Select Case TableGen.CodeType
            Case CodeTypeEnum.VB2005
                sb.AppendLine("End Class")
            Case CodeTypeEnum.CS2005
                sb.AppendLine("}")
        End Select

        'code.InnerText = Output
        Return sb.ToString
    End Function

    Public Function GetDetailASPXPageLoad(ByVal TableGen As RawCreateScript) As String
        Dim sb As New StringBuilder

        Dim Spaces As New Space(1)

        GetPageLoad(sb, TableGen, Spaces)


        Select Case TableGen.CommentLevel
            Case CommentLevelEnum.Basic

            Case CommentLevelEnum.Verbose
                AddComment(sb, TableGen, Spaces, " ")
                AddComment(sb, TableGen, Spaces, "A postback is when the user clicks a button after the page has loaded")
                AddComment(sb, TableGen, Spaces, "if it's false then we do our one time load of data her if it exists")

        End Select
     
        Spaces.Inc()
        sb.Append(GetIsPostbackEqualFalse(Spaces, TableGen))
        Spaces.Inc()



        'marker
        LoadSingleRecordControls(Spaces, sb, TableGen)

        'endmarker

        sb.AppendLine(Spaces.ToString & "LoadThemes()" & EndStatement(TableGen))
        Spaces.Dec()


        Select Case TableGen.CommentLevel
            Case CommentLevelEnum.Basic
                AddComment(sb, TableGen, Spaces, " ")
                AddComment(sb, TableGen, Spaces, "end of ID > 0")

            Case CommentLevelEnum.Verbose
                AddComment(sb, TableGen, Spaces, " ")
                AddComment(sb, TableGen, Spaces, "end of ID > 0")

        End Select
        GetEndIf(sb, Spaces, TableGen)
        Spaces.Dec()


        Select Case TableGen.CommentLevel
            Case CommentLevelEnum.Basic

            Case CommentLevelEnum.Verbose
                AddComment(sb, TableGen, Spaces, " ")
                AddComment(sb, TableGen, Spaces, "End of Page_Load Event Handler")

        End Select

        GetEndSub(sb, Spaces, TableGen)

        LoadThemesFunction(Spaces, sb, TableGen, False)

        LoadSubComboBoxInitializeCodeVB(Spaces, sb, TableGen, "")

        Spaces.Dec()
        sb.AppendLine()
        Return sb.ToString
    End Function

    Public Function GetDetailASPXCancelClick(ByVal TableGen As RawCreateScript) As String
        Dim sb As New StringBuilder
        sb.AppendLine()
        Select Case TableGen.CodeType
            Case CodeTypeEnum.VB2005
                sb.AppendLine("    Protected Sub cmdCancel_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmdCancel.Click")
            Case CodeTypeEnum.CS2005
                sb.AppendLine("    protected void cmdCancel_Click(object sender, EventArgs e)")
                sb.AppendLine("    {")
        End Select

        sb.Append(EditVBReturnToMaster(TableGen))

        Select Case TableGen.CodeType
            Case CodeTypeEnum.VB2005
                sb.AppendLine("    End Sub")
            Case CodeTypeEnum.CS2005
                sb.AppendLine("    }")
        End Select

        Return sb.ToString
    End Function

 
  
End Class
